/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-04<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
package cn.cnsy123.bookwebsiteserver.controller.dict;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.cnsy123.bookwebsiteserver.entity.SysParamInfo;
import cn.cnsy123.bookwebsiteserver.param.DictInsertParam;
import cn.cnsy123.bookwebsiteserver.param.DictUpdateParam;
import cn.cnsy123.bookwebsiteserver.service.dict.DictInfoService;
import cn.cnsy123.bookwebsiteserver.vo.layui.CommonResult;
import cn.cnsy123.bookwebsiteserver.vo.layui.TableResult;

/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-04<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
@Controller
public class DictInfoController {
    
    private static final Logger logger = LoggerFactory.getLogger(DictInfoController.class);
    
    @Autowired
    private DictInfoService dictInfoService;

    @GetMapping(value = "/dictManager")
    public ModelAndView toDictPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("dictManager");
        modelAndView.setStatus(HttpStatus.OK);
        return modelAndView;
    }
    
    @GetMapping(value = "/dict/list")
    @ResponseBody
    public TableResult<SysParamInfo> findAllDictConfigInfo(
            @RequestParam(value = "dictType",required = false,defaultValue = "") String dictType,
            @RequestParam(value = "page",required = true,defaultValue = "1") Integer page,
            @RequestParam(value = "limit",required = true,defaultValue = "10") Integer size){
        TableResult<SysParamInfo> tableResult = null;
        logger.info("字典配置信息查询,请求携带的字典名称:{}",dictType);
        try {
            tableResult = dictInfoService.findAllDictConfigInfo(page.intValue(),size.intValue(),dictType);
        } catch (Exception e) {
            logger.error("字典信息获取失败",e);
            //记录错误信息到数据库
            //返回默认错误处理信息
            tableResult = new TableResult<SysParamInfo>(0,"信息获取失败，请通知系统管理员",0L,null);
        }
        return tableResult;
    }
    
    @DeleteMapping(value = "/dict/del/{dictId}")
    @ResponseBody
    public CommonResult<Object> deleteDictItemInfo(@PathVariable(value = "dictId",required = true) Long dictId){
        CommonResult<Object> result = null;
        try {
            result = dictInfoService.deleteDictItemInfo(dictId);
        }catch(Exception e) {
            result = new CommonResult<>(1, "删除字典项失败");
        }
        return result;
    }
    
    @PostMapping(value = "/dict/update")
    @ResponseBody
    public CommonResult<Object> updateDictItemInfo(DictUpdateParam param){
        CommonResult<Object> result = null;
        try {
            result = dictInfoService.updateDictItemInfo(param);
        }catch(Exception e) {
            result = new CommonResult<>(1, "更新字典项失败");
        }
        return result;
    }
    
    @PutMapping(value = "/dict/insert")
    @ResponseBody
    public CommonResult<Object> insertDictItemInfo(DictInsertParam param){
        CommonResult<Object> result = null;
        try {
            result = dictInfoService.insertDictItemInfo(param);
        }catch(Exception e) {
            result = new CommonResult<>(1, "新增字典项失败");
        }
        return result;
    }
}
